<template>
  <draggable id="addForm" class="addalarmrecord">
    <h3 style="text-align: center">New Alarm Record</h3>
    <el-form ref="form" :model="form" label-width="80px" @submit.native.prevent>
      <el-form-item prop="name" label="Name" style="width: 80%">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="Status">
        <el-select v-model="form.status" placeholder="Status" clearable>
          <el-option label="Open" value="Open"></el-option>
          <el-option label="Pending" value="Pending"></el-option>
          <el-option label="Closed" value="Closed"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="CreateTime">
        <el-col :span="11">
          <el-date-picker
            type="datetime"
            placeholder="Choose Time"
            v-model="form.gentime"
            style="width: 100%"
            @change="timePicker"
          ></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="Owner" style="width: 300px">
        <el-input v-model="form.handleby"></el-input>
      </el-form-item>
      <el-form-item label="From">
        <el-radio-group v-model="form.resource">
          <el-radio label="DV"></el-radio>
          <el-radio label="I2000"></el-radio>
          <el-radio label="Others"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="Progress" style="width: 90%">
        <el-input type="textarea" v-model="form.progress"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">Create</el-button>
        <el-button @click="doClose">Cancel</el-button>
      </el-form-item>
    </el-form>
  </draggable>
</template>

<script>
import { createAlarm } from "@/api/alarm.js";
import draggable from "vuedraggable";
export default {
  name: "AddAlarmRecord",
  props: {
    dialog_visible: Boolean,
  },

  components: { draggable },
  data() {
    return {
      visible: this.dialog_visible,
      form: {
        name: "",
        status: "Open",
        gentime: this.formateDate(new Date(), "yyyy-MM-dd hh:mm:ss"),
        progress: "",
        handleby: "xxx",
        resource: "DV",
      },
    };
  },
  watch: {
    visible(val) {
      console.log("val :>> ", val);
      this.dialog_visible = val;
    },
  },
  computed: {},
  methods: {
    formateDate(date, format) {
      // console.log("date :>> ", date);
      // console.log("format :>> ", format);
      let time = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "h+": date.getHours(),
        "m+": date.getMinutes(),
        "s+": date.getSeconds(),
        "q+": Math.floor((date.getMonth() + 3) / 3),
        "S+": date.getMilliseconds(),
      };
      if (/(y+)/i.test(format)) {
        format = format.replace(
          RegExp.$1,
          (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      }
      for (let k in time) {
        if (new RegExp("(" + k + ")").test(format)) {
          format = format.replace(
            RegExp.$1,
            RegExp.$1.length === 1
              ? time[k]
              : ("00" + time[k]).substr(("" + time[k]).length)
          );
        }
      }
      console.log("format :>> ", format);
      return format;
    },
    timePicker() {
      this.form.gentime = this.formateDate(
        this.form.gentime,
        "yyyy-MM-dd hh:mm:ss"
      );
    },
    onSubmit() {
      if (!this.form.name) {
        alert("Alarm name can not put empty");
        return;
      }
      createAlarm(this.form).then((res) => {
        this.$store.state.alarmRecordList.push(this.form);
        this.$store.state.alarmRecordListSave = this.$store.state.alarmRecordList.slice(
          0
        );
        // console.log("res.message :>> ", res.message);
        this.$emit("dialogVisibleEvent", false);
      });
    },
    doClose() {
      // 触发父组件中的dialogVisibleEvent事件，并传递参数
      this.$emit("dialogVisibleEvent", false);
    },
  },
  created() {},
};
</script>

<style scoped>
.addalarmrecord {
  margin: 10px;
  border: red 1px solid;
  width: 800px;
  background-color: #f1f5f5;
  position: absolute;
  top: 135px;
  left: 288px;
}
</style>